@echo off
setlocal enabledelayedexpansion

set COMMAND=
set MODULE=
set PROFILE=prod

cd /d %~dp0..
set MY_HOME=%cd%
cd "..\RuoYi-Cloud-Plus"
set RY_HOME=%cd%
cd "..\plus-ui"
set RY_UI_HOME=%cd%
cd /d "%~dp0.."
set JAVA_HOME=C:\Program Files\Java\graalvm-jdk-17.0.11+7.1
set PATH=%JAVA_HOME%\bin;%PATH%

set VALID_COMMANDS=pull package login deploy
set VALID_MODULES=gateway auth system resource job workflow gen monitor snailjob seata ui

:parse
if "%~1"=="" goto execute
if not defined COMMAND (
    set "COMMAND=%~1"
    shift
    goto parse
)
if "%~1"=="-t" (
    set PROFILE=prod
    shift
    goto parse
) else if "%~1"=="-p" (
    set PROFILE=prod
    shift
    goto parse
)
if not defined MODULE (
    set "MODULE=%~1"
    shift
    goto parse
)
goto :usage

:execute
set IS_VALID_COMMAND=0
for %%c in (%VALID_COMMANDS%) do (
    if "!COMMAND!"=="%%c" set IS_VALID_COMMAND=1
)
if !IS_VALID_COMMAND!==0 (
    echo Error: unknown command "!COMMAND!"
    goto :usage
)
if not "!MODULE!"=="" (
    set IS_VALID_MODULE=0
    for %%m in (%VALID_MODULES%) do (
        if "!MODULE!"=="%%m" set IS_VALID_MODULE=1
    )
    if !IS_VALID_MODULE!==0 (
        echo Error: unknown module "!MODULE!"
        goto :usage
    )
)
if "!COMMAND!"=="pull" (
    call :pull
) else if "!COMMAND!"=="package" (
    call :package
) else if "!COMMAND!"=="login" (
    call :login
) else if "!COMMAND!"=="deploy" (
    call :deploy
)
goto :eof

:pull
if "!MODULE!"=="" (
    git pull
) else if "!MODULE!"=="ui" (
    cd /d "!RY_UI_HOME!"
    git pull
) else (
    cd /d "!RY_HOME!"
    git pull
)
goto :eof

:package
if "!MODULE!"=="" (
    cd /d "!RY_HOME!"
    mvn clean package -Dmaven.test.skip=true -P%PROFILE%
) else if "!MODULE!"=="ui" (
    call "%PROFILE%\%MODULE%\package.bat" %RY_UI_HOME% %MY_HOME%
) else (
    call "%PROFILE%\%MODULE%\package.bat" %RY_HOME% %MY_HOME%
)
goto :eof

:login
call az acr login -n regwmsuat
goto :eof

:deploy
if "!MODULE!"=="" (
    echo Need a module
) else if "!MODULE!"=="ui" (
    call "%PROFILE%\%MODULE%\deploy.bat"
) else (
    call "%PROFILE%\%MODULE%\deploy.bat" %RY_HOME% %MY_HOME%
)
goto :eof

:usage
echo.
echo Usage:
echo   nimbus ^<COMMAND^> [MODULE] [PROFILE]
echo.
echo Command:
echo   %VALID_COMMANDS%
echo.
echo Module:
echo   %VALID_MODULES%
echo.
echo Profile:
echo   -t -p
echo.
goto :eof

endlocal
exit /b 1
